/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import clases.ClsDatabase;
import clases.ClsMetodos;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author usuario
 */
public class dlgCapitulo extends javax.swing.JDialog {

    private ClsMetodos metodos = new ClsMetodos();
    private ClsDatabase bd = new ClsDatabase();
    DefaultComboBoxModel modelGrupoCombo = new DefaultComboBoxModel();
    int a = 1;

    /**
     * Creates new form dlgCapitulo2
     */
    public dlgCapitulo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        inicia();
    }

    public void inicia() {
        setLocationRelativeTo(null);//Centramos la ventana
        metodos._iconJDialog(this);


        metodos.tablaOcultarcolumnas(tblTipoProyecto, new int[]{0});
        a = 1;
        cargar_grupos();
        cargar_TipoProyecto("");
        a = 2;
    }

    public void cargar_grupos() {
        metodos.comboboxLlenarMas(cboGrupo, modelGrupoCombo, "SELECT idcapitulo_base_grupo , nombre FROM capitulo_base_grupo ");
    }

    public void cargar_TipoProyecto(String nombre) {
        String val_grupo = modelGrupoCombo.getElementAt(cboGrupo.getSelectedIndex()).toString();
        metodos.tablaLlenaSql(tblTipoProyecto,
                "idcapitulo_base,nombre",
                "FROM capitulo_base "
                + "WHERE nombre like '%" + nombre + "%' and  idcapitulo_base_grupo  like '%" + val_grupo + "%' "
                + "ORDER BY nombre asc ");
    }

    public void ver(int accion) {
        if (accion == 1) {
            int fila = cboGrupo.getSelectedIndex();
            String codigo = "";
            if (fila > 0) {
                codigo = modelGrupoCombo.getElementAt(cboGrupo.getSelectedIndex()).toString();

            }
            new dlgCapituloEditar(null, true, accion, "", codigo).setVisible(true);

        } else if (accion == 2) {
            int fila = tblTipoProyecto.getSelectedRow();
            if (fila != -1) {
                String codigo = tblTipoProyecto.getValueAt(fila, 0).toString();
                new dlgCapituloEditar(null, true, accion, codigo, "").setVisible(true);
            } else {
                javax.swing.JOptionPane.showMessageDialog(this, "Seleccione un Título");
            }

        }
        cargar_TipoProyecto(jTextField1.getText());
    }

    public void ver_grupo(int accion) {
        if (accion == 1) {
            new dlgCapituloGrupoEditar(null, true, accion, "").setVisible(true);
        } else if (accion == 2) {
            int fila = cboGrupo.getSelectedIndex();
            if (fila > 0) {
                String codigo = modelGrupoCombo.getElementAt(cboGrupo.getSelectedIndex()).toString();
                new dlgCapituloGrupoEditar(null, true, accion, codigo).setVisible(true);
            } else {
                javax.swing.JOptionPane.showMessageDialog(this, "Seleccione un Grupo");
            }
        }
        cboGrupo.setSelectedIndex(0);
        cargar_grupos();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTipoProyecto = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        cboGrupo = new javax.swing.JComboBox();
        btnCrear1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        btnEliminar1 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Títulos");

        jPanel1.setBackground(new java.awt.Color(225, 235, 247));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tblTipoProyecto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Nombre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblTipoProyecto.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblTipoProyecto.getTableHeader().setReorderingAllowed(false);
        tblTipoProyecto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblTipoProyectoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblTipoProyecto);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 131, 590, 180));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar"));
        jPanel4.setOpaque(false);
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Nombre:");
        jPanel4.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 25, 80, 25));

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });
        jPanel4.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 25, 260, -1));

        jLabel19.setText("Grupo Títulos:");
        jPanel4.add(jLabel19, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 80, 25));

        cboGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboGrupoActionPerformed(evt);
            }
        });
        jPanel4.add(cboGrupo, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 60, 260, -1));

        btnCrear1.setText("Crear");
        btnCrear1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCrear1ActionPerformed(evt);
            }
        });
        jPanel4.add(btnCrear1, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 60, 60, -1));

        jButton4.setText("Editar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 60, 60, -1));

        jButton3.setText("Eliminar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 60, 75, -1));

        jPanel1.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, 590, 100));

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/agregar.png"))); // NOI18N
        btnAgregar.setText("Crear");
        btnAgregar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        jPanel1.add(btnAgregar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 330, -1, -1));

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/editar.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        jPanel1.add(btnEditar, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 330, -1, -1));

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/duplicar.png"))); // NOI18N
        btnEliminar.setText("Duplicar");
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        jPanel1.add(btnEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 330, -1, -1));

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/salir.png"))); // NOI18N
        jButton5.setText("Salir");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 330, 102, -1));

        btnEliminar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/peque/borrar.png"))); // NOI18N
        btnEliminar1.setText("Eliminar");
        btnEliminar1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnEliminar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminar1ActionPerformed(evt);
            }
        });
        jPanel1.add(btnEliminar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 330, -1, -1));

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/fondo_dialog.jpg"))); // NOI18N
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 450, 330));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 620, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        cargar_TipoProyecto(jTextField1.getText());
    }//GEN-LAST:event_jTextField1KeyReleased

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        ver(1);
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        ver(2);
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        int fila = tblTipoProyecto.getSelectedRow();
        if (fila != -1) {
            String codigo = tblTipoProyecto.getValueAt(fila, 0).toString();
            new dlgCapituloEditar(null, true, 4, codigo, "").setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "Seleccione un Título");
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void btnCrear1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCrear1ActionPerformed
        ver_grupo(1);
    }//GEN-LAST:event_btnCrear1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        ver_grupo(2);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int fila = cboGrupo.getSelectedIndex();
        if (fila > 0) {
            if (JOptionPane.showConfirmDialog(null, "Está seguro que desea eliminar el Grupo de Título seleccionado", "Mensaje", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
                String codigo = modelGrupoCombo.getElementAt(cboGrupo.getSelectedIndex()).toString();
                dlgCapituloGrupoEditar dialog = new dlgCapituloGrupoEditar(null, true, 3, codigo);
                dialog.eliminar();
                cargar_grupos();
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(this, "Seleccione un Grupo");
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void cboGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboGrupoActionPerformed
        if (a == 2) {
            if (cboGrupo.getSelectedIndex() != -1) {
                cargar_TipoProyecto(jTextField1.getText());
            }
        }
    }//GEN-LAST:event_cboGrupoActionPerformed

    private void btnEliminar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminar1ActionPerformed
        int fila = tblTipoProyecto.getSelectedRow();
        if (fila != -1) {
            if (JOptionPane.showConfirmDialog(null, "Está seguro que desea eliminar el Título seleccionado", "Mensaje", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
                String codigo = tblTipoProyecto.getValueAt(fila, 0).toString();
                dlgCapituloEditar dialog = new dlgCapituloEditar(null, true, 3, codigo, "");
                dialog.eliminar();
                cargar_TipoProyecto(jTextField1.getText());
            }
        } else {
            JOptionPane.showMessageDialog(null, "Seleccione un Título");
        }

    }//GEN-LAST:event_btnEliminar1ActionPerformed

    private void tblTipoProyectoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTipoProyectoMouseClicked
        if (evt.getClickCount() == 2) {
            ver(2);
        }
    }//GEN-LAST:event_tblTipoProyectoMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgCapitulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgCapitulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgCapitulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgCapitulo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgCapitulo dialog = new dlgCapitulo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCrear1;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnEliminar1;
    private javax.swing.JComboBox cboGrupo;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTable tblTipoProyecto;
    // End of variables declaration//GEN-END:variables
}
